<?php
/**
 * User: robinfai
 * Date: 15/1/20
 * Time: 23:28
 * @var $this HomeController
 * @var $log ProfilingLog
 *
 */

?><!DOCTYPE html>
<html>
<!-- MEMO: update me with `git checkout gh-pages && git merge master && git push origin gh-pages` -->
<head>
    <meta charset="utf-8">
    <title>Profiler</title>
    <link href="<?php echo $this->module->assetsUrl ?>/css/bootstrap.css" rel="stylesheet">
    <link href="<?php echo $this->module->assetsUrl ?>/bootstrap-material-design/dist/css/material.css"
          rel="stylesheet">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
        .modal.in .modal-dialog {
            z-index: 1045;
        }

        .code-container {
            background: #fff;
            padding: 0;
            border: none;
        }
    </style>
</head>
<body>
<div class="navbar navbar-default">
    <div class="container">

        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse"
                    data-target=".navbar-responsive-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="javascript:void(0)">Profiler</a>
        </div>
        <div class="navbar-collapse collapse navbar-responsive-collapse">
            <ul class="nav navbar-nav">
                <li class="active"><a href="<?php echo $this->createUrl('index') ?>">首页</a></li>
            </ul>
        </div>
    </div>
</div>
<div class="container well">

    <table class="table table-bordered">
        <tr>
            <td>Controller</td>
            <td><?php echo $log->controller ?></td>
        </tr>
        <tr>
            <td>action</td>
            <td><?php echo $log->action ?></td>
        </tr>
        <tr>
            <td>log time</td>
            <td><?php echo $log->logTime ?></td>
        </tr>
        <tr>
            <td>profiling time</td>
            <td><?php echo $log->profilingTime ?></td>
        </tr>
        <tr>
            <td>GET 参数</td>
            <td>
                <pre class="code-container"><code
                        class="php"><?php var_export(json_decode($log->getParams, true)) ?></code></pre>
            </td>
        </tr>
        <tr>
            <td>POST 参数</td>
            <td>
                <pre class="code-container"><code
                        class="php"><?php var_export(json_decode($log->postParams, true)) ?></code></pre>
            </td>
        </tr>
    </table>
    <h3>profiling log</h3>

    <div id="profiling-tree" class="well well-sm">
    </div>

</div>
<div class="modal" id="code-context">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">代码片段</h4>
            </div>
            <div class="modal-body">
                <pre class="code-container"><code class="php"></code></pre>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
<?php
$cs = Yii::app()->clientScript;
$cs->registerScriptFile($this->module->assetsUrl . '/jquery-1.10.2.min.js');
$cs->registerScriptFile($this->module->assetsUrl . '/bootstrap.min.js');
$cs->registerScriptFile($this->module->assetsUrl . '/jstree/dist/jstree.js');
$cs->registerCssFile($this->module->assetsUrl . '/jstree/dist/themes/default/style.min.css');

$cs->registerScriptFile($this->module->assetsUrl . '/highlight/highlight.pack.js');
$cs->registerCssFile($this->module->assetsUrl . '/highlight/styles/tomorrow.css');
?>
<script>
    $(function () {
        hljs.initHighlightingOnLoad();
        $('pre code').each(function (i, block) {
            hljs.highlightBlock(block);
        });
        var profilingLog = <?php echo $log->profilingLog?>;
        var setNode = function (data) {
            var time = ((data['end_time'] - data['begin_time']) * 1000).toFixed(2);
            return {
                "text": data['token'] + ":" + time + 'ms',
                "left": data['left'],
                'id': data['left'],
                "right": data['right'],
                "file": data['begin_file'],
                "begin_line": data['begin_line'],
                "end_line": data['end_line'],
                "children": !!(data['right'] - data['left'] - 1)
            };
        };
        $('#profiling-tree').jstree({
            'core': {
                'data': function (node, cb) {
                    if (node.id === "#") {
                        var data = [
                            setNode(profilingLog[0])
                        ];

                        cb(data);
                    }
                    else {

                        var data = [], current = node.original.left;

                        $.each(profilingLog, function (k, v) {
                            if ((v['left'] - current) == 1) {
                                data.push(setNode(profilingLog[k]))
                                current = profilingLog[k]['right'];
                            }
                        });
                        if (data.length)
                            cb(data);
                    }
                }
            }
        });
        $('#profiling-tree').on("changed.jstree", function (e, data) {
            var row = data.node.original;
            $.getJSON('<?php echo $this->createUrl('getFileContent')?>', {
                file: row.file,
                beginLine: row.begin_line,
                endLine: row.end_line
            }, function (json) {
                $('#code-context pre code').text(json.content);
                hljs.highlightBlock($('#code-context pre code')[0]);
                $('#code-context').modal();
            })
        });
    });
</script>
</body>
</html>